// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { CardIdEnum } from "../../Enums/GameEnum";
import { getTimehms, getZeroTime } from "../../GlobalFunction";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import CardManager, { CardEvent } from "../../Models/Cards/CardManager";
import BaseUI from "../BaseUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MonthCardUI extends BaseUI {
    @property(cc.Button)
    receiveBtn: cc.Button = null;
    @property(cc.Button)
    buyBtn: cc.Button = null;
    @property(cc.Label)
    cdTxt: cc.Label = null;
    @property(cc.Toggle)
    toggle0: cc.Toggle = null;
    @property(cc.Toggle)
    toggle1: cc.Toggle = null;

    leftTime: number;
    protected onEnable(): void {
        var userdata = SaveManager.single.userdata;

        if (userdata.monthCard != null && cc.sys.now() - userdata.monthCard.receiveTime >= 24 * 60 * 60 * 1000) {
            userdata.monthCard.isReceive = false;
            userdata.monthCard.receiveTime = getZeroTime();
        }

        this.drawMonthView();
        GameEvent.eventNode.on(CardEvent.CARDBUY_EVENT, this.onUpdata, this);
        GameEvent.eventNode.on(CardEvent.CARDRECEIVE_EVENT, this.onUpdata, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(CardEvent.CARDBUY_EVENT, this.onUpdata, this);
        GameEvent.eventNode.off(CardEvent.CARDRECEIVE_EVENT, this.onUpdata, this);
    }
    onUpdata() {
        this.drawMonthView();
    }

    drawMonthView() {
        var userdata = SaveManager.single.userdata;
        if (userdata.monthCard == null || userdata.monthCard.isOutDate()) {
            this.toggle0.isChecked = this.toggle1.isChecked = false;
            this.buyBtn.node.active = true;
            this.receiveBtn.node.active = false;
            this.cdTxt.string = "";
        } else {
            this.toggle0.isChecked = true;
            this.toggle1.isChecked = userdata.monthCard.isReceive;
            this.buyBtn.node.active = false;
            this.receiveBtn.node.active = true;
            this.receiveBtn.interactable = !userdata.monthCard.isReceive;
            this.leftTime = userdata.monthCard.leftTime;
            this.cdTxt.string = "剩余时间：" + getTimehms(this.leftTime);
        }
    }

    buyClick() {
        CardManager.single.requestBuyCard(CardIdEnum.monthId);
    }
    receiveClick() {
        var userdata = SaveManager.single.userdata;
        if (!userdata.monthCard) {
            return;
        }
        if (userdata.monthCard.isReceive) {
            UIManager.single.showTips("今日已领取。");
            return;
        }
        CardManager.single.requestReceive(CardIdEnum.monthId);
    }

    protected update(dt: number): void {
        if (this.leftTime > 0) {
            this.leftTime -= dt;
            this.cdTxt.string = "剩余时间：" + getTimehms(this.leftTime);
        }
    }


}
